package addressBook;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

/**
 * The view of the address-book's main window.
 * 
 * @param contacts dictionary of contacts
 * @param panel main panel
 * @param menuBar menu bar
 * @param file file menu
 */
public class simpleWindowView extends JFrame implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public contacts myContacts;
	
    private JPanel panel;
	private JMenuBar menuBar;
    private JMenu file;
    private JMenu edit;
    private JMenu help;
    JMenuItem aFileItem;
    JMenuItem sEditItem;
    JMenuItem rEditItem;
    JMenuItem svFileItem;
    JMenuItem lFileItem;
    JMenuItem eFileItem;
    private JMenu editSort;
    JCheckBoxMenuItem fSortItem;
    JCheckBoxMenuItem lSortItem;
    JCheckBoxMenuItem pSortItem;
    JCheckBoxMenuItem aSortItem;
    JCheckBoxMenuItem cSortItem;
    JCheckBoxMenuItem uSortItem;
    JCheckBoxMenuItem eSortItem;
    private JTable table;
    private JList list;
    private DefaultListModel listModel = new DefaultListModel();
    private JScrollPane spTable;
    private JScrollPane spList;
    String[] header = {"First Name", "Last Name", "Phone Number", "Address", "City", "Url", "Email"};
    contact selectedContact;
    
    private JLabel label1 = new JLabel("First name:");
	JTextField textField1 = new JTextField(10);
	private JLabel label2 = new JLabel("Last name:");
	JTextField textField2 = new JTextField(10);
	private JLabel label3 = new JLabel("Phone number:");
	JTextField textField3 = new JTextField(10);
	private JLabel label4 = new JLabel("Street address:");
	JTextField textField4 = new JTextField(10);
	private JLabel label5 = new JLabel("City:");
	JTextField textField5 = new JTextField(10);
	private JLabel label6 = new JLabel("Url:");
	JTextField textField6 = new JTextField(10);
	private JLabel label7 = new JLabel("E-mail:");
	JTextField textField7 = new JTextField(10);
	JButton button = new JButton("Update contact");
    
    public simpleWindowView(contacts toContacts) {
    	    	
    	myContacts = toContacts;
    	
    	setTitle("Address Book");
        setSize(1100, 730);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
    	
        /*textField1.setEditable(false);
        textField2.setEditable(false);*/
    	panel = new JPanel();
    	menuBar = new JMenuBar();
    	file = new JMenu("File");
    	edit = new JMenu("Edit");
    	help = new JMenu("Help");
    	aFileItem = new JMenuItem("Add contact");
    	sEditItem = new JMenuItem("Search contact");
    	rEditItem = new JMenuItem("Remove contact");
    	svFileItem = new JMenuItem("Save contacts");
    	lFileItem = new JMenuItem("Load contacts");
    	eFileItem = new JMenuItem("Exit");
    	editSort = new JMenu("Sort by");
    	fSortItem = new JCheckBoxMenuItem("First Name");
    	lSortItem = new JCheckBoxMenuItem("Last Name");
    	pSortItem = new JCheckBoxMenuItem("Phone number");
    	aSortItem = new JCheckBoxMenuItem("Address");
    	cSortItem = new JCheckBoxMenuItem("City");
    	uSortItem = new JCheckBoxMenuItem("Url");
    	eSortItem = new JCheckBoxMenuItem("Email");
    	
    	lSortItem.setState(true);
    	
    	setTable(new JTable(myContacts.getContacts(), header ){
    		
    		/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
    		public boolean isCellEditable(int row, int col) {
    		     switch (col) {
    		         default:
    		             return false;
    		      }
    		     
    		}
    	});
    	getTable().getTableHeader().setReorderingAllowed(false);
    	getTable().getTableHeader().setResizingAllowed(false);
    	spTable = new JScrollPane(getTable());
    	myContacts.setListModel(getListModel());
    	setList(new JList(myContacts.getListModel()));
    	spList = new JScrollPane(getList());
        
        panel.setLayout(new BorderLayout());
    	
    	JPanel editPanel = new JPanel(new SpringLayout());
    	JPanel buttonPanel = new JPanel(new BorderLayout());
    	JPanel modifyPanel = new JPanel(new BorderLayout());
 
		editPanel.add(label1);
		textField1.setBackground(Color.gray.brighter());
		label1.setLabelFor(textField1);
		editPanel.add(textField1);
		editPanel.add(label2);
		textField2.setBackground(Color.gray.brighter());
		label2.setLabelFor(textField2);
		editPanel.add(textField2);
		editPanel.add(label3);
		label3.setLabelFor(textField3);
		editPanel.add(textField3);
		editPanel.add(label4);
		label4.setLabelFor(textField4);
		editPanel.add(textField4);
		editPanel.add(label5);
		label5.setLabelFor(textField5);
		editPanel.add(textField5);
		editPanel.add(label6);
		label6.setLabelFor(textField6);
		editPanel.add(textField6);
		editPanel.add(label7);
		label7.setLabelFor(textField7);
		editPanel.add(textField7);
		editPanel.add(button);
		
        SpringUtilities.makeGrid(editPanel, 7, 2, 0, 0, 0, 0);
		
		editPanel.setOpaque(true);

        file.setMnemonic(KeyEvent.VK_F);
    	eFileItem.setMnemonic(KeyEvent.VK_E);
        aFileItem.setMnemonic(KeyEvent.VK_E);
        sEditItem.setMnemonic(KeyEvent.VK_E);
        rEditItem.setMnemonic(KeyEvent.VK_E);
        svFileItem.setMnemonic(KeyEvent.VK_E);
        lFileItem.setMnemonic(KeyEvent.VK_E);
        edit.setMnemonic(KeyEvent.VK_F);
        editSort.setMnemonic(KeyEvent.VK_F);
    	fSortItem.setMnemonic(KeyEvent.VK_E);
    	lSortItem.setMnemonic(KeyEvent.VK_E);
    	pSortItem.setMnemonic(KeyEvent.VK_E);
    	aSortItem.setMnemonic(KeyEvent.VK_E);
    	cSortItem.setMnemonic(KeyEvent.VK_E);
    	uSortItem.setMnemonic(KeyEvent.VK_E);
    	eSortItem.setMnemonic(KeyEvent.VK_E);
        help.setMnemonic(KeyEvent.VK_F);
        
        file.add(aFileItem);
        edit.add(rEditItem);
        edit.add(sEditItem);
        file.add(svFileItem);
        file.add(lFileItem);
        file.add(eFileItem);
        
        editSort.add(fSortItem);
        editSort.add(lSortItem);
        editSort.add(pSortItem);
        editSort.add(aSortItem);
        editSort.add(cSortItem);
        editSort.add(uSortItem);
        editSort.add(eSortItem);
        
        edit.add(editSort);
        
        menuBar.add(file);
        menuBar.add(edit);
        menuBar.add(help);
        
        button.setEnabled(false);

        panel.add(menuBar, BorderLayout.PAGE_START);
        panel.add(spList, BorderLayout.WEST);
        panel.add(spTable, BorderLayout.SOUTH);
		buttonPanel.add(button, BorderLayout.CENTER);
        
		modifyPanel.add(buttonPanel, BorderLayout.SOUTH);
		modifyPanel.add(editPanel, BorderLayout.NORTH);
		
		panel.add(modifyPanel, BorderLayout.EAST);
        this.add(panel);
    
    }
    
    void updateListener(ActionListener listenForUpdateButton){
    	
    	button.addActionListener(listenForUpdateButton);
    	
    }

    void listListener(ListSelectionListener listenForListClick){
		
		list.addListSelectionListener(listenForListClick);
		
	}
    
    void addNewContactListener(ActionListener listenForAddButton){
		
		aFileItem.addActionListener(listenForAddButton);
		
	}
    
    void sortByFirstListener(ActionListener listenForFirstButton){
		
		fSortItem.addActionListener(listenForFirstButton);
		
	}
    
    void sortByLastListener(ActionListener listenForLastButton){
		
		lSortItem.addActionListener(listenForLastButton);
		
	}
    
    void sortByPhoneListener(ActionListener listenForPhoneButton){
		
		pSortItem.addActionListener(listenForPhoneButton);
		
	}
    
    void sortByAddressListener(ActionListener listenForAddressButton){
		
		aSortItem.addActionListener(listenForAddressButton);
		
	}
    
    void sortByCityListener(ActionListener listenForCityButton){
		
		cSortItem.addActionListener(listenForCityButton);
		
	}
    
    void sortByUrlListener(ActionListener listenForUrlButton){
		
		uSortItem.addActionListener(listenForUrlButton);
		
	}
    
    void sortByEmailListener(ActionListener listenForEmailButton){
		
		eSortItem.addActionListener(listenForEmailButton);
		
	}
    
    void selectedRow(MouseListener listenForSelectedRow){
    	getTable().addMouseListener(listenForSelectedRow);
    }
    
    void selectedKeyboardRow(KeyListener listenForSelectedRow){
    	getTable().addKeyListener(listenForSelectedRow);
    }

	public void selectedListKeyBoardRow(KeyListener listenForDelete) {
		
		getList().addKeyListener(listenForDelete);
		
	}
    
    void tableModificationListener(TableCellListener tcl){
    	
    	Action action = new AbstractAction()
    	{
    	    /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e)
    	    {

				TableCellListener tcl = (TableCellListener)e.getSource();
				if(tcl.getOldValue()!=null){
					if(tcl.getColumn()==0||tcl.getColumn()==1){
						if(tcl.getColumn()==0){
							String fN = tcl.getNewValue().toString();
							String lN = myContacts.getContacts()[tcl.getRow()][1].toString();
							fN = fN.concat(" ").concat(lN);
							Object fnLn = fN;
							myContacts.modifyContact(fnLn, tcl.getRow());
					        myContacts.sortContacts(myContacts.getArg());
						}
						else{
							String fN = myContacts.getContacts()[tcl.getRow()][0].toString();
							String lN = tcl.getNewValue().toString();
							fN = fN.concat(" ").concat(lN);
							Object fnLn = fN;
							myContacts.modifyContact(fnLn, tcl.getRow());
					        myContacts.sortContacts(myContacts.getArg());
						}
					}
				}
			}
    	};
        tcl = new TableCellListener(table, action);
        myContacts.sortContacts(myContacts.getArg());
    }
    
    void searchContactListener(ActionListener listenForSearchButton){
		
		sEditItem.addActionListener(listenForSearchButton);
		
	}
    
    void removeContactListener(ActionListener listenForRemoveButton){
		
		rEditItem.addActionListener(listenForRemoveButton);
		
	}
    
    void saveContactsListener(ActionListener listenForSaveButton){
		
		svFileItem.addActionListener(listenForSaveButton);
		
	}

    void loadListener(ActionListener listenForLoadClick){
		
		lFileItem.addActionListener(listenForLoadClick);
		
	}
    
    void exitListener(ActionListener listenForExitButton){
		
		eFileItem.addActionListener(listenForExitButton);
		
	}
    
	void displayErrorMessage(IllegalArgumentException ex){
		
		JOptionPane.showMessageDialog(this, ex.getMessage());
		
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}
	
	public JList getList() {
		return list;
	}

	public void setList(JList list) {
		list.setSelectedIndex(0);
		this.list = list;
	}
	
	public DefaultListModel getListModel() {
		return listModel;
	}

	public void setListModel(DefaultListModel listModel) {
		this.listModel = listModel;
	}
	
	public contact getSelectedContact() {
		return selectedContact;
	}

	public void setSelectedContact(contact selectedContact) {
		this.selectedContact = selectedContact;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
    	getTable().repaint();
		getList().repaint();
		if(getTable().getSelectedRow()<myContacts.numContacts()&&getList().getSelectedIndex()!=-1){
			setSelectedContact(new contact(myContacts.getContacts()[getList().getSelectedIndex()]));
			textField1.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][0]);
			textField2.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][1]);
			textField3.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][2]);
			textField4.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][3]);
			textField5.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][4]);
			textField6.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][5]);
			textField7.setText(myContacts.getContacts()[myContacts.idContact(getSelectedContact())][6]);
		}
		else if(getTable().getSelectedRow() == -1){
			getList().clearSelection();
			getTable().clearSelection();
			textField1.setText("");
			textField2.setText("");
			textField3.setText("");
			textField4.setText("");
			textField5.setText("");
			textField6.setText("");
			textField7.setText("");
		}
		
	}

}

class PopUpMenu extends JPopupMenu {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JMenuItem rItem;
    public PopUpMenu(){
        rItem = new JMenuItem("Remove Me!");
        add(rItem);
    }
    
    void removeContactListener(ActionListener listenForRemoveButton){
		
		rItem.addActionListener(listenForRemoveButton);
		
	}
}